/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.yuhanginfo.modules.sys.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.yuhanginfo.common.config.Global;
import com.yuhanginfo.common.persistence.Page;
import com.yuhanginfo.common.utils.FileUtils;
import com.yuhanginfo.common.utils.StringUtils;
import com.yuhanginfo.common.web.BaseController;
import com.yuhanginfo.modules.sys.entity.Carousel;
import com.yuhanginfo.modules.sys.service.CarouselService;
import com.yuhanginfo.modules.sys.utils.UserUtils;

/**
 * 轮播图Controller
 * @author lvls
 * @version 2017-10-09
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/carousel")
public class CarouselController extends BaseController {

	private static final  Logger logger = LoggerFactory.getLogger(CarouselController.class);
	
	@Autowired
	private CarouselService carouselService;
	
	private static final  String REDIRECT = "redirect:"+Global.getAdminPath()+"/sys/carousel/?repage";
	
	@ModelAttribute
	public Carousel get(@RequestParam(required=false) String id) {
		Carousel entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = carouselService.get(id);
		}
		if (entity == null){
			entity = new Carousel();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:carousel:view")
	@RequestMapping(value = {"list", ""})
	public String list(Carousel carousel, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Carousel> page = carouselService.findPage(new Page<Carousel>(request, response), carousel); 
		model.addAttribute("page", page);
		return "modules/sys/carousel/carouselList";
	}

	@RequiresPermissions("sys:carousel:view")
	@RequestMapping(value = "form")
	public String form(Carousel carousel, Model model) {
		model.addAttribute("carousel", carousel);
		return "modules/sys/carousel/carouselForm";
	}
	
	@RequiresPermissions("sys:carousel:view")
	@RequestMapping(value = "edit")
	public String edit(Carousel carousel, Model model) {
		model.addAttribute("carousel", carousel);
		return "modules/sys/carousel/carouselEdit";
	}

	@RequiresPermissions("sys:carousel:edit")
	@RequestMapping(value = "save")
	public String save(Carousel carousel,@RequestParam("file") MultipartFile[] file, Model model, RedirectAttributes redirectAttributes) {
		StringBuilder path = new StringBuilder();
		try {
			for (MultipartFile f : file) {
				String originalFilename = f.getOriginalFilename();
				path.append(FileUtils.saveSimpleNewImage(UserUtils.getUser().getId(), originalFilename, f.getInputStream()));
			}
		} catch (Exception ex) {
			logger.info("save",ex);
		}
		carousel.setImg(path.toString());
		carouselService.save(carousel);
		addMessage(redirectAttributes, "保存轮播图成功");
		return REDIRECT;
	}
	
	@RequiresPermissions("sys:carousel:edit")
	@RequestMapping(value = "editSave")
	public String editSave(Carousel carousel,@RequestParam("file") MultipartFile[] file, Model model, RedirectAttributes redirectAttributes) {
		StringBuilder path = new StringBuilder();
		try {
			for (MultipartFile f : file) {
				String originalFilename = f.getOriginalFilename();
				if(originalFilename==""){
					path.append(carousel.getImg());
				}else{
					path.append(FileUtils.saveSimpleNewImage(UserUtils.getUser().getId(), originalFilename, f.getInputStream()));
				}
			}
		} catch (Exception ex) {
			logger.info("editSave",ex);
		}
		carousel.setImg(path.toString());
		carouselService.save(carousel);
		addMessage(redirectAttributes, "保存轮播图成功");
		return REDIRECT;
	}
	
	@RequiresPermissions("sys:carousel:edit")
	@RequestMapping(value = "delete")
	public String delete(Carousel carousel, RedirectAttributes redirectAttributes) {
		carouselService.delete(carousel);
		addMessage(redirectAttributes, "删除轮播图成功");
		return REDIRECT;
	}

}